/*
 * AfterSaleInfoLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 申请售后的商品信息的Layout */
public class AfterSaleInfoLayout extends ViewGroup {

    /** 垂直间隔,默认15dp */
    private int mVerticalSpacing = UIUtils.dp2px(0);

    public AfterSaleInfoLayout(Context context) {
        super(context);
    }

    public AfterSaleInfoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = resolveSize(0, widthMeasureSpec);

        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        // 开始计算高度 需要加上paddingTop
        int childTop = paddingTop;

        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            // 测量 Child
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            // 获取Child的高度
            int childHeight = childView.getMeasuredHeight();

            if (i < getChildCount() - 1) {
                childTop += childHeight + mVerticalSpacing;
            } else {
                childTop += childHeight;
            }
        }

        // 最后加上paddingBottom
        int heightSize = childTop + paddingBottom;

        setMeasuredDimension(widthSize, resolveSize(heightSize, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();

        int childLeft = paddingLeft;
        int childTop = paddingTop;

        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);

            if (childView.getVisibility() == GONE) {
                continue;
            }

            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();

            childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);

            childTop = childTop + childHeight + mVerticalSpacing;
        }
    }

    /** 给Layout设置数据 */
    public void setData(List<Goods> data) {
        removeAllViews();
        if (data != null && data.size() != 0) {
            for (int i = 0; i < data.size(); i++) {
                AfterSaleItemView view = new AfterSaleItemView(getContext());
                Goods goods = data.get(i);
                String goodsNumber = goods.getGoodsNumber() + "";
                view.setMaxAndMin(goodsNumber,goods.getMinNumber());
                view.setNumber(UIUtils.getString(R.string.shopping_cart_count, goodsNumber));
                view.setName(goods.getGoodsName());
                view.setGoodsLogo(getContext(), goods.getGoodsThumb());
                view.setPrice(goods.getFormatedGoodsPrice());
                view.setDialogData(
                        UIUtils.getString(R.string.exchange_after_sale_changeNum),
                        UIUtils.getString(R.string.exchange_after_sale_tip3, goodsNumber));
                addView(view);
            }
        }
    }
}
